﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

using BLL;
using GodHelper.Models;
using GodHelper.Util;

namespace GodHelper.Controllers
{
    public class View_MainController : FilterController
    {
        public UserModel userModel = null;

        public ActionResult test()
        {
            return View();
        }

        public ActionResult Main(string name)
        {
            string Useremail = null;
            string AutoLogin = null;
            if (Request.Cookies["AutoLogin"] != null)
            {
                AutoLogin = Request.Cookies["AutoLogin"].Value.ToString();
            }
            if (Request.Cookies["Useremail"] != null)
            {
                Useremail = Request.Cookies["Useremail"].Value.ToString();
            }

            if (AutoLogin == null)
            {
                if (Session["Useremail"] == null)
                {
                    Response.Redirect("/View_Login/Login");
                    return null;
                }

                string str = name;
                userModel = new UserModel
                {
                    UserModel_userinfo = Business.LoadMange.Load_userifno(str),
                    UserModel_past_task = Business.LoadMange.Load_past_task(str),
                    UserModel_ing_task = Business.LoadMange.Load_ing_task(str),
                    UserModel_finish_task = Business.LoadMange.Load_finish_task(str),
                    UserModel_contact = Business.LoadMange.Load_contact(str),
                    UserModel_product = Business.LoadMange.Load_product(str),
                    UserModel_trade = Business.LoadMange.Load_trade(str)
                };
                UserModelManager.instance.add(str, userModel);
            }
            else
            {
                if (Useremail != null)
                {
                   string str = Useremail;
                    //if (UserModelManager.instance.ContainKey(str))
                    //{
                    //    userModel = UserModelManager.instance.get(str);
                    //}
                    //else
                    //{
                        userModel = new UserModel
                        {
                            UserModel_userinfo = Business.LoadMange.Load_userifno(str),
                            UserModel_past_task = Business.LoadMange.Load_past_task(str),
                            UserModel_ing_task = Business.LoadMange.Load_ing_task(str),
                            UserModel_finish_task = Business.LoadMange.Load_finish_task(str),
                            UserModel_contact = Business.LoadMange.Load_contact(str),
                            UserModel_product = Business.LoadMange.Load_product(str),
                            UserModel_trade = Business.LoadMange.Load_trade(str)
                        };
                        UserModelManager.instance.add(str, userModel);
                    //}
                }
                else
                {
                    Response.Redirect("/View_Login/Login");
                    return null;
                }
            }
            return View(userModel);         
            
        }
 


        /// <summary>
        /// 登录后主显页判断
        /// </summary>
        /// <returns></returns>
        //[HttpPost]
        public ActionResult Jmp_UserCenter_or_Task(string email)
        {
            //UserModel userModel = null;

            //if (Session["Useremail"] != null)
            //{
            //    string str = Session["Useremail"].ToString();
            //    userModel = new UserModel { UserModel_userinfo = Business.LoadMange.Load_userifno(str) };
            //}
            //if (userModel.UserModel_userinfo._user_age == null) //还未填写详细用户资料
            //{
            //    return Json(new { Result = true });
            //}
            //if (Session["AutoLogin"] == null)
            //{
            //    Session["Useremail"] = null;
            //}
            return Json(new { Result = false });
        }

    }
}
